package com.hyperion.blog.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hyperion.blog.dao.mapper.ArticleMapper;
import com.hyperion.blog.dao.mapper.SysUserMapper;
import com.hyperion.blog.dao.pojo.Article;
import com.hyperion.blog.dao.pojo.SysUser;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author Hyperion
 */
@Component
public class ThreadService {

    @Async("taskExecutor")
    public void updateViewCount(ArticleMapper articleMapper, Article article) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getId, article.getId());
        article.setViewCounts(article.getViewCounts() + 1);
        articleMapper.update(article, queryWrapper);
    }

    @Async("taskExecutor")
    public void updateLastLogin(SysUserMapper sysUserMapper, SysUser sysUser) {
        sysUser.setLastLogin(System.currentTimeMillis());
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getId, sysUser.getId());
        sysUserMapper.update(sysUser, queryWrapper);
    }

}
